﻿using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Sucdri.Infrastructure.Core.Context
{
   internal class DataAccessContext
   {
      private DbConnection _dbConnection;
      private readonly Database _database;
      private DbTransaction _dbTransaction;

      public DataAccessContext(Database database)
      {
         _database = database;
      }

      public Database Database
      {
         get { return _database; }
      }

      public DbTransaction Transaction 
      { 
         get { return _dbTransaction; }
      }

      public void BeginTransaction()
      {
         _dbConnection = Database.CreateConnection();
         _dbConnection.Open();
         _dbTransaction = _dbConnection.BeginTransaction(IsolationLevel.ReadCommitted);
      }

      public void Rollback()
      {
         _dbTransaction.Rollback();
      }

      public void Commit()
      {
         _dbTransaction.Commit();
      }

      public void CloseConnection()
      {
         if (_dbConnection.State == ConnectionState.Open)
            _dbConnection.Close();
      }
   }
}